package com.ys.web.ws.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.ys.web.entity.Company;
import com.ys.web.entity.Industry;
import com.ys.web.pojo.CompanyInfo;
import com.ys.web.remotemodel.CompanyIndustry;
import com.ys.web.service.CompanyService;
import com.ys.web.service.IndustryService;
import com.ys.web.ws.service.CompanyInfoRemoteService;




public class CompanyInfoRemoteServiceImpl implements CompanyInfoRemoteService{
	
	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private IndustryService industryService;

	@Override
	public List<CompanyIndustry> getCompanyInfoList() {
//		List<Industry> industrys = industryService.getIndustryByIsSynchronizedToYsb("0");
		List<CompanyInfo> allCompanyInfos = companyService.getCompanyNotIsSynchronized();
		List<CompanyIndustry> companyIndustrys = new ArrayList<CompanyIndustry>(); 
//		logger.info(industrys.size());
//		for(Industry industry:industrys){
//			logger.info(industry.getId());
//			List<CompanyInfo> companyInfos= companyService.getCompanyByIndustry(industry.getId());
//			
//			if(companyInfos!=null){
//				logger.info("公司数："+companyInfos.size());
//				allCompanyInfos.addAll(companyInfos);
//			}
//			
//		}
		
		for(CompanyInfo companyInfo:allCompanyInfos){
			CompanyIndustry companyIndustry = new CompanyIndustry();
			companyIndustry.setCompanyName(companyInfo.getCompanyName());
			companyIndustry.setIndustry(companyInfo.getIndustry());
			companyIndustrys.add(companyIndustry);
		}
		
			return companyIndustrys;
	}
	
	
	public List<CompanyInfo> getAllCompanyInfo() {
		List<CompanyInfo> allCompanyInfos = null;
		try {
			allCompanyInfos = companyService.getAllCompanyInfo();
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allCompanyInfos;
		
	}

	@Override
	public void saveSynchronizedIndustry(List<String> industryNames) {
			for(String industryName:industryNames){
				Industry industry = industryService.getIndustryByIndustryName(industryName);
				industry.setIsIndustryToYsbSynchronized("1");
				industryService.saveOrUpdate(industry);
			}
			
		
		
		
	}

	@Override
	public List<String> getIndustryIsNotSynchronized() {
		List<Industry> industrys = industryService.getIndustryByIsSynchronizedToYsb("0");
		List<String> industryNames = new ArrayList<String>();
		for(Industry industry:industrys){
			
			industryNames.add(industry.getIndustryName());
		}
		return industryNames;
	}

	@Override
	public void saveSynchronizedCompany(List<String> companyNames) {
		for(String companyName:companyNames){
			if(null!=companyName){
			Company company;
			try {
					company = companyService.getCompanyByCompanyName(companyName);
					company.setIsSynchronizedToYsb("1");
					companyService.saveCompany(company);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}		
		}	
	}
		
		
	
	
	

}
